<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="EAN码"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eancode">
              <a-tooltip>
                <template slot="title">
                  用户名的描述信息
                </template>
              <a-input v-model="model.eancode" placeholder="产品条形码"></a-input>
              </a-tooltip>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="电压类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="voltyp">
              <j-dict-select-tag type="list" v-model="model.voltyp" dictCode="mes_partno_tech_cfg@VOLTYP"
                                 placeholder="请选择电压类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="直流电压" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dcvol">
              <a-input v-model="model.dcvol" placeholder="请输入直流电压" suffix="V"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="直流电流" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dccur">
              <a-input v-model="model.dccur" placeholder="请输入直流电流" suffix="A"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="直流分段能力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dcir">
              <a-input v-model="model.dcir" placeholder="请输入直流分段能力" suffix="KA"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="交流电压" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="acvol">
              <a-input v-model="model.acvol" placeholder="请输入交流电压" suffix="V"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="交流电流" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accur">
              <a-input v-model="model.accur" placeholder="请输入交流电流" suffix="A"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="交流分段能力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="acir">
              <a-input v-model="model.acir" placeholder="请输入交流分段能力" suffix="KA"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="温度转换系数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tempcoef">
              <a-input v-model="model.tempcoef" placeholder="请输入温度转换系数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="标准一" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="std1">
              <a-input v-model="model.std1" placeholder="请输入标准一"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="标准二" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="std2">
              <a-input v-model="model.std2" placeholder="请输入标准二"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="标准三" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="std3">
              <a-input v-model="model.std3" placeholder="请输入标准三"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="标准四" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="std4">
              <a-input v-model="model.std4" placeholder="请输入标准四"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="标准五" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="std5">
              <a-input v-model="model.std5" placeholder="请输入标准五"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="标准六" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="std6">
              <a-input v-model="model.std6" placeholder="请输入标准六"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="保护特性类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pctyp">
              <a-input v-model="model.pctyp" placeholder="请输入保护特性类型"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="客户特别定制" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="custspec">
              <a-input v-model="model.custspec" placeholder="请输入客户特别定制"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comments">
              <a-input v-model="model.comments" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesPartnoTechCfgModal',
  components: {},
  props: {
    partNo: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      isEdit: false,
      title: '操作',
      width: 1300,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        eancode: [
          { required: true, message: '请输入产品条形码!' }
        ],
        voltyp: [
          { required: true, message: '请输入电压类型!' }
        ],
        op: [
          { required: true, message: '请输入需要上料的工序!' }
        ],
        lottrack: [
          { required: true, message: '请输入是否需要扣减物料批次!' }
        ]
      },
      url: {
        add: '/basic/mesPartnoTechCfg/add',
        edit: '/basic/mesPartnoTechCfg/edit'
      }

    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model['partno'] = this.partNo
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>
